package springboot.client;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.endpoint.Client;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import springboot.kernel.KernelTools;
import springboot.wstool.WSTools;

import java.util.Date;


@Service
@Slf4j
public class TsakStatusAndAgvStatus {
    WSTools wsTools=new WSTools();
    KernelTools km = new KernelTools();
    String v1OrderName=null;
    int A1=0;
    int B1=0;
    int C1=0;
    int D1=0;
    public static final String V1 = "Vehicle-0001";
    @Scheduled(fixedDelay = 1000)
    public void changeTaskStatus() throws Exception {
        if (D1==0){
            v1OrderName=km.getTranspotOrder(V1);
            A1=0;B1=0;C1=0;
        }
        if(!v1OrderName.equals("N/A")) {
            if(!v1OrderName.startsWith("Park") && !v1OrderName.startsWith("Recharge")
                    && !v1OrderName.startsWith("TOrder") && !v1OrderName.startsWith("Move")) {
                String orderName=km.getPropertyByOrder(v1OrderName,"orderName");
                if (D1==0){
                    D1=1;
                    /*此处任务状态是30,表示已开始*/
                    /*Object[] objects = new Object[0];
                    Client client=wsTools.getClient();
                    objects=client.invoke("",orderName,"30","0001",new Date(),"0");
                    System.out.println("返回数据:" + objects[0]);*/
                    log.info("任务状态是30,表示已开始");
                }
                if(A1==0&&km.getDriveOrderOperation(v1OrderName).startsWith("Unload")&&
                        km.getDriveOrderState(v1OrderName).equals("TRAVELLING")&&
                        //并且当前点位不等于放货进叉点位
                        !km.getPosition("Vehicle-0001").equals(km.getDriverOrderPoint(v1OrderName))
                ){
                    /*此处任务状态是32,表示已开始*/
                    /*Object[] objects = new Object[0];
                    Client client=wsTools.getClient();
                    objects=client.invoke("",orderName,"32","0001",new Date(),"0");
                    System.out.println("返回数据:" + objects[0]);*/
                    log.info("任务状态是32,表示已取货");
                    A1=1;
                }
                String v1orderStatus = km.getOrderStatus(v1OrderName);
                if(B1==0&&v1orderStatus.equals("FINISHED")) {
                    /*此处任务状态是40,表示已开始*/
                    /*Object[] objects = new Object[0];
                    Client client=wsTools.getClient();
                    objects=client.invoke("",orderName,"40","0001",new Date(),"1");
                    System.out.println("返回数据:" + objects[0]);*/
                    log.info("任务状态是40,表示已完成");
                    B1=1;
                    D1=0;
                }
                if(C1==0&&v1orderStatus.equals("FAILED")) {
                    /*此处任务状态是99,表示已开始*/
                    /*Object[] objects = new Object[0];
                    Client client=wsTools.getClient();
                    objects=client.invoke("",orderName,"99","0001",new Date(),"1");
                    System.out.println("返回数据:" + objects[0]);*/
                    log.info("任务状态是99,表示已失败");
                    C1=1;
                    D1=0;
                }
            }
        }
    }
    @Scheduled(fixedDelay = 1000)
    public void changeAgvStatus() throws Exception{
        String state = km.getVehicleStatus(V1);//获得车的状态	   //state,procstate来判断状态
        String procstate = km.getProcState(V1);//获得当前车执行的状态(通过State与Processing state来判断)
        String level = km.getinter(V1);//获得该车的调用级别
        String orderName=km.getTranspotOrder(V1);
        if((state.equals("IDLE") && procstate.equals("IDLE") && level.equals("TO_BE_UTILIZED"))
                || (state.equals("EXECUTING") && orderName.startsWith("Recharge") && level.equals("TO_BE_UTILIZED"))
                || (state.equals("EXECUTING") && orderName.startsWith("Park") && level.equals("TO_BE_UTILIZED"))
                || (state.equals("CHARGING") && level.equals("TO_BE_UTILIZED"))) {
            //0表示当前无任务处于待机状态
            log.info("无任务待机状态");
        } else if (state.equals("EXECUTING") && orderName.startsWith("TO")) {
            /*Object[] objects = new Object[0];
            Client client=wsTools.getClient();
            objects=client.invoke("",orderName,"0001","EXECUTING",new Date());
            System.out.println("返回数据:" + objects[0]);*/
            //1表示正在执行(包括OPENTCS发任务以及WMS发的任务)
            log.info("正在执行任务");
        } else if (state.equals("ERROR")) {
            /*Object[] objects = new Object[0];
            Client client=wsTools.getClient();
            objects=client.invoke("",orderName,"0001","ERROR",new Date());
            System.out.println("返回数据:" + objects[0]);*/
            //获得一车错误信息
            log.info("AGV报错");
        } else if (state.equals("UNKNOWN")) {
            //无法知道具体点位
            /*Object[] objects = new Object[0];
            Client client=wsTools.getClient();
            objects=client.invoke("",orderName,"0001","UNKNOWN",new Date());
            System.out.println("返回数据:" + objects[0]);*/
            log.info("AGV状态为UNKNOWN");
        }

    }
}
